package com.huangmb.anymark.server.controller;

import com.alibaba.fastjson.JSON;
import com.huangmb.anymark.server.dto.Response;
import com.huangmb.anymark.server.model.User;
import com.huangmb.anymark.server.service.UserService;
import com.huangmb.anymark.server.utils.Constant;
import com.huangmb.anymark.server.utils.SessionUtils;
import com.huangmb.anymark.server.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 用户相关控制器
 * @author hmb
 * @version 1.0
 * @since 2016/5/3 22:04
 */
@RestController
@RequestMapping("/user")
@Scope("prototype")
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource(name = "userService")
    private UserService mUserService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Response login(@RequestParam String name, @RequestParam String password, HttpSession session) {

        logger.debug("name:" + name + ";password:" + password);
        if (StringUtils.isBlank(name) || StringUtils.isBlank(password)){
            return new Response(Response.CODE_NAME_OR_PWD_ERROR, Response.MSG_NAME_OR_PWD_ERROR, null);
        }
        User user = mUserService.findByName(name);
        if (user != null && user.getPassword().equals(password)){//验证通过
            SessionUtils.clear(session);
            session.setAttribute(Constant.SESSION_USER,user);
            return Response.buildSuccess(JSON.toJSONString(user));
        }
        return new Response(Response.CODE_NAME_OR_PWD_ERROR, Response.MSG_NAME_OR_PWD_ERROR, null);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Response register(User user,HttpSession session) {
        if (mUserService.findByName(user.getName()) != null) {
            return new Response(Response.CODE_NAME_OR_PWD_ERROR, Response.MSG_NAME_EXIST, null);
        }
        logger.debug("user" + user.getName());
        mUserService.save(user);
        String res = JSON.toJSONString(user);
        logger.debug(res);
        SessionUtils.clear(session);
        session.setAttribute(Constant.SESSION_USER,user);
        return Response.buildSuccess(res);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Response logout(HttpSession session) {
        SessionUtils.clear(session);
        return Response.buildSuccess(null);
    }

    @RequestMapping( method = RequestMethod.PUT)
    public Response update(User user,HttpSession session) {
        System.out.println(user.getId()+user.getName());
        User user1 = (User) session.getAttribute(Constant.SESSION_USER);
        if (user1 == null) {
            logger.debug("未登录");
            return new Response(Response.CODE_AUTH_FAIL, Response.MSG_AUTH_FAIL, null);
        }

        User oldUser = mUserService.getById(user.getId());
        if(oldUser == null || !oldUser.getId().equals(user1.getId())){
            return new Response(Response.CODE_BAD_PARAMETER,Response.MSG_BAD_PARAMETER,null);
        }
        if (StringUtils.isNotBlank(user.getName())){
            oldUser.setName(user.getName().trim());
        }
        if ((StringUtils.isNotBlank(user.getPassword()))){
            oldUser.setPassword(user.getPassword().trim());
        }
        if ((StringUtils.isNotBlank(user.getAvatar()))){
            oldUser.setAvatar(user.getAvatar().trim());
        }
        if(user.getSex() != null){
            oldUser.setSex(user.getSex());
        }
        mUserService.update(oldUser);
        String res = JSON.toJSONString(oldUser);
        return Response.buildSuccess(res);
    }
}
